package com.rran.study.concurrent.safeend;

/**
 * @author yy
 * @Type EndThread.java
 * @Desc 如何安全中断线程
 * @date 2020/7/29 11:46
 */
public class EndThread {

    private static class MyThread extends Thread{

        public MyThread(String name){
            super(name);
        }

        @Override
        public void run() {
            String threadName = this.getName();
            System.out.println(threadName+" interrrupt flag ="+isInterrupted());
            while(!isInterrupted()){
                System.out.println(threadName+"线程正在执行");
            }
            System.out.println(threadName+"线程被中断");
            System.out.println(threadName+" interrrupt flag ="+isInterrupted());
        }
    }
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new MyThread("myThread");
        thread.start();
        Thread.sleep(200);
        thread.interrupt();//中断线程，其实设置线程的标识位true
    }

}
